﻿@{
    ViewData["Title"] = L("LiveLog");
}

<!-- live-logs start -->
<h2 class="page-header">@L("LiveLog")</h2>
<div class="row">
    <div class="col-md-4">
        Number of entries to show
        <select value.bind="numberOfEntriesToShow">
            <option value="25">25</option>
            <option value="50">50</option>
            <option value="100">100</option>
        </select>
    </div>
    <div class="col-md-4">
        <label for="showTriggerInfo">@L("ShowTriggerInfo")</label>
        <input type="checkbox" id="showTriggerInfo" />
        <label for="showJobInfo">@L("ShowJobInfo")</label>
        <input type="checkbox" id="showJobInfo" />
    </div>
</div>

<div class="row">
    <div class="col-sm-12">
        <pre style="word-wrap: normal; overflow: auto; height: 100%"><code repeat.for="entry of entries">[${entry.date | dateFormat:'YYYY-MM-DD HH:mm:ss'}] <span innerHtml.bind="entry.message"></span> <br /></code></pre>
    </div>
</div>

<script>

    $showTriggerInfo = $('#showTriggerInfo');
    $showJobInfo = $('#showJobInfo');
    var entries = [];
    var numberOfEntriesToShow = 50;

    function showMessage(message) {

        while (entries.length >= numberOfEntriesToShow) {
            entries.shift();
        }

        const value = {
            date: moment(),
            message: message
        };
        entries.push(value);
        console.log(entries);
    }

    var connection = new signalR.HubConnectionBuilder().withUrl("/api/liveLogHub").build();

    connection.on("TriggerFired",
        (trigger) => {
            console.log(trigger);
            console.log($showTriggerInfo.val());
                if ($showTriggerInfo.val())
                    showMessage(`Trigger <strong>${trigger.name}.${trigger.group}</strong> fired`);
            });
    connection.on("TriggerMisfired",
        (trigger) => {
            if ($showTriggerInfo.val())
                showMessage(`<strong>Trigger ${trigger.name}.${trigger.group} misfired</strong>`);
        });
    connection.on("TriggerCompleted",
        (trigger) => {
            if ($showTriggerInfo.val())
                showMessage(`Trigger <strong>${trigger.name}.${trigger.group}</strong> has completed`);
        });
    connection.on("TriggerPaused",
        (triggerKey) => {
            if ($showTriggerInfo.val())
                showMessage(`Trigger <strong>${triggerKey.name}.${triggerKey.group}</strong> was paused`);
        });
    connection.on("TriggerResumed",
        (triggerKey) => {
            if ($showTriggerInfo.val())
                showMessage(`Trigger <strong>${triggerKey.name}.${triggerKey.group}</strong> was resumed`);
        });
    connection.on("JobPaused",
        (jobKey) => {
            if ($showJobInfo)
                showMessage(`Job <strong>${jobKey.name}.${jobKey.group}</strong> was paused`);
        });
    connection.on("JobResumed",
        (jobKey) => {
            if ($showJobInfo)
                showMessage(`Job <strong>${jobKey.name}.${jobKey.group}</strong> was resumed`);
        });
    connection.on("JobToBeExecuted",
        (jobKey, triggerKey) => {
            if ($showJobInfo)
                showMessage(
                    `Starting to execute job <strong>${jobKey.name}.${jobKey.group
                    }</strong> triggered by trigger <strong>${triggerKey.name}.${triggerKey.group}</strong>...`);
        });
    connection.on("JobWasExecuted",
        (jobKey, triggerKey, errorMessage) => {
            if ($showJobInfo) {
                let message = `Job <strong>${jobKey.name}.${jobKey.group}</strong> was executed`;
                if (errorMessage) {
                    message += ` and ended with error: ${errorMessage}`;
                }
                showMessage(message);
            }
        });

    connection.start().then(function () {
        showMessage("Connected");
    }).catch(function (err) {
        showMessage("SignalR error: Could not start hub connection");
    });

    window.onbeforeunload=function(e) {
        connection.stop();
    }
</script>
<!-- live-logs end -->